<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label-width="120px" label="预归档档案名称" prop="fileArchiveName">
        <el-input
          v-model="queryParams.fileArchiveName"
          placeholder="请输入预归档档案名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> 
      <el-form-item label-width="120px" label="预归档档案类型" prop="archiveTypeId">
        <el-select v-model="queryParams.archiveTypeId" placeholder="请选择档案类型" clearable>
          <el-option
            v-for="dict in dict.type.archive_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-if="menu === 'arch'"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:DFile:add']"
        >进行档案预归档</el-button>
      </el-col>
      
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:DFile:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:DFile:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="DFileList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="档案预归档申请编号" align="center" prop="id" />
      <el-table-column label="预归档档案名称" align="center" prop="fileArchiveName" />
      <el-table-column label="预归档档案类型" align="center" prop="archiveType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.archive_type" :value="scope.row.archiveTypeId"/>
        </template>
      </el-table-column>
      <el-table-column label="企业名称" align="center" prop="enterpriseName" />
      <el-table-column label="企业编号" align="center" prop="enterpriseId" />
      <el-table-column label="部门名称" align="center" prop="departmentName" />
      <el-table-column label="部门编号" align="center" prop="departmentId" />
      <el-table-column label="档案预归档申请人名称" align="center" prop="createBy" />
      <el-table-column label="档案预归档申请人编号" align="center" prop="userId" />
      <el-table-column label="预归档档案内容" align="center" prop="fileArchiveContent" />
      <!-- <el-table-column label="预归档档案地址" align="center" prop="fileArchiveAddress" /> -->
          <el-table-column
        label="文件"
        align="center"
        prop="filePath"
        min-width="300px"
        show-overflow-tooltip
      >
      <template slot-scope="scope">
          <el-tag type="info" v-if="scope.row.fileArchiveAddress == null || scope.row.fileArchiveAddress === ''">无文件</el-tag>
          <div v-if="scope.row.fileArchiveAddress != null && scope.row.fileArchiveAddress !== ''">
            <li
              :key="path"
              class="el-upload-list__item ele-upload-list__item-content"
              v-for="path in getFilePath(scope.row.fileArchiveAddress)"
            >
              <el-link :href="path" :underline="false" target="_blank">
                <span class="el-icon-document">
                  {{
                  getFileName(path)
                  }}
                </span>
              </el-link>
            </li>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="预归档档案的所属案卷名称" align="center" prop="docName" />
      <el-table-column label="预归档档案的所属案卷编号" align="center" prop="docNumber" />
      <el-table-column label="预归档档案的保管期限" align="center" prop="saveTime" />
      <el-table-column label="是否可借阅(与保密等级有关，机密不可借阅，解密可以借阅)" align="center" prop="borrowState" />
      <el-table-column label="保密等级" align="center" prop="secrecyLevel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.secret_level" :value="scope.row.secrecyLevel"/>
        </template>
      </el-table-column>
      <el-table-column label="盒号" align="center" prop="boxNumber" />
      <el-table-column label="纸质档案存放地址" align="center" prop="location" />
      <el-table-column label="审批状态" align="center" prop="shenpiState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.apply_status" :value="scope.row.shenpiState"/>
        </template>
      </el-table-column>
      <el-table-column label="企业级档案管理员的名称" align="center" prop="enterpriseAdminName" />
      <el-table-column label="企业级档案管理员的编号" align="center" prop="enterpriseAdminId" />
      <el-table-column label="企业级档案管理员的审批意见" align="center" prop="enterpriseAdminOpinion">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.approval_opinion" :value="scope.row.enterpriseAdminOpinion"/>
        </template>
      </el-table-column>
      <el-table-column label="企业级档案管理员的备注说明" align="center" prop="enterpriseAdminNote" />
      <el-table-column label="政府级档案管理员的名称" align="center" prop="governmentAdminName" />
      <el-table-column label="政府级档案管理员的编号" align="center" prop="governmentAdminId" />
      <el-table-column label="政府级档案管理员的审批意见" align="center" prop="governmentAdminOpinion">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.approval_opinion" :value="scope.row.governmentAdminOpinion"/>
        </template>
      </el-table-column>
      <el-table-column label="政府级档案管理员的备注说明" align="center" prop="governmentAdminNote" />
      <el-table-column v-if="menu !== 'arch'" fixed="right" label="操作" width="200px" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:DFile:edit']"
          >修改</el-button> -->
        <el-button
          v-if="menu === 'en'"
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          @click="handleUpdateEnterprise(scope.row)"
          v-hasPermi="['system:DFile:edit']"
        >企业审批</el-button>
        <el-button
          v-if="menu === 'gv'"
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          @click="handleUpdateGovernment(scope.row)"
          v-hasPermi="['system:DFile:edit']"    
        >政府审批</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:DFile:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 进行档案预归档对话框 -->
    <el-dialog  v-if="title === '档案预归档'" :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="档案名称" prop="archiveId">
          <el-select v-model="form.archiveId" filterable clearable placeholder="请选择">
            <el-option
              v-for="item in arOptions"
              :key="item.id"
              :label="item.archiveName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="档案类型" prop="archiveType">
          <el-radio-group v-model="form.archiveType" placeholder="请选择档案类型">
            <el-radio
              v-for="dict in dict.type.archive_type"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item> -->
        <!-- <el-form-item label="企业名称（读取企业级管理员对应的企业）" prop="enterpriseName">
          <el-txet v-model="form.enterpriseName" placeholder="请输入企业名称" />
        </el-form-item>
        <el-form-item label="企业id(通过名称找到)" prop="enterpriseId">
          <el-txet v-model="form.enterpriseId" placeholder="请输入企业id" />
        </el-form-item> -->
        <!-- <el-form-item label="部门名称" prop="departmentName">
          <el-radio-group v-model="form.departmentName" placeholder="请选择部门名称">
            <el-radio
              v-for="dict in dict.type.department_name"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item> -->
        <!-- <el-form-item label="提交人(通过名称找到)" prop="userId">
          <el-txet v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="提交人名称（读取企业级管理员的名字）" prop="userName">
          <el-txet v-model="form.userName" placeholder="请输入用户名称" />
        </el-form-item> -->
        <!-- <el-form-item label="档案内容简述" prop="archiveContent">
          <el-input v-model="form.archiveContent" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="档案文件地址" prop="archiveAddress">
          <el-input v-model="form.archiveAddress" placeholder="请输入档案文件地址" />
        </el-form-item>
        <el-form-item label="所属案卷名" prop="docName">
          <el-input v-model="form.docName" placeholder="请输入所属案卷名" />
        </el-form-item>
        <el-form-item label="所属案卷编号" prop="docNumber">
          <el-input v-model="form.docNumber" placeholder="请输入所属案卷编号" />
        </el-form-item>
        <el-form-item label="保管期限" prop="saveTime">
          <el-input v-model="form.saveTime" placeholder="请输入保管期限" />
        </el-form-item> -->
        <!-- <el-form-item label="是否可借阅" prop="borrowState">
          <el-input v-model="form.borrowState" placeholder="请输入是否可借阅，与保密等级有关，机密不可借阅，解密可以借阅" />
        </el-form-item> -->
        <!-- <el-form-item label="保密等级" prop="secrecyLevel">
          <el-radio-group v-model="form.secrecyLevel" placeholder="请选择档案类型">
            <el-radio
              v-for="dict in dict.type.secrecy_level"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="档案盒号" prop="boxNumber">
          <el-input v-model="form.boxNumber" placeholder="请输入档案盒号" />
        </el-form-item>
        <el-form-item label="纸质档案存放地址" prop="location">
          <el-input v-model="form.location" placeholder="请输入纸质档案存放的现实地址" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    
    <!-- 审批档案预归档对话框 -->
    <el-dialog  v-if="title === '企业审批档案预归档'" :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="160px">
        <el-form-item label="保密等级" prop="secrecyLevel">
          <el-radio-group v-model="form.secrecyLevel" placeholder="请选择档案类型">
            <el-radio
              v-for="dict in dict.type.secret_level"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="企业级档案管理员意见" prop="enterpriseAdminOpinion">
          <el-radio-group v-model="form.enterpriseAdminOpinion" placeholder="请选择档案类型">
            <el-radio
              v-for="dict in dict.type.approval_opinion"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="企业级管理员备注" prop="enterpriseAdminNote">
          <el-input v-model="form.enterpriseAdminNote" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog  v-if="title === '政府审批档案预归档'" :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="160px">
        <el-form-item label="政府级档案管理员意见" prop="governmentAdminOpinion">
          <el-radio-group v-model="form.governmentAdminOpinion">
            <el-radio
              v-for="dict in dict.type.approval_opinion"
              :key="dict.label"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="政府级管理员备注" prop="governmentAdminNote">
          <el-input v-model="form.governmentAdminNote" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDFile, getDFile, delDFile, addDFile, updateDFile } from "@/api/system/DFile";
import { listDArchive, getDArchive, delDArchive, addDArchive, updateDArchive } from "@/api/system/DArchive";
import { getInfo } from "@/api/login";
export default {
  name: "DFile",
  dicts: ['apply_status','archive_type','approval_opinion','department_name', 'secret_level'],
  data() {
    return {
      // 项目根路径
      baseUrl: process.env.VUE_APP_BASE_API,
      menu: null,
      user: null,
      arOptions: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 档案预归档表格数据
      DFileList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fileArchiveName: null,
        archiveType: null,
        enterpriseId: null,
        departmentId: null,
        userId: null,
        fileArchiveContent: null,
        fileArchiveAddress: null,
        docName: null,
        docNumber: null,
        saveTime: null,
        borrowState: null,
        secrecyLevel: null,
        boxNumber: null,
        location: null,
        shenpiState: null,
        enterpriseAdminId: null,
        enterpriseAdminName: null,
        enterpriseAdminOpinion: null,
        enterpriseAdminNote: null,
        governmentAdminId: null,
        governmentAdminName: null,
        governmentAdminOpinion: null,
        governmentAdminNote: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getArchList();
    getInfo().then(res => {
      this.user = res.user;
    });
      // 获取菜单信息
    this.menu = this.$route.query.menu;
  },
  methods: {
    /** 获取档案下拉菜单 */
    getArchList() {
      listDArchive({'archStatus': 0, pageNum: 1,  pageSize: 100}).then(response => {
        this.arOptions = response.rows;
      });
    },
    /** 查询档案预归档列表 */
    getList() {
      this.loading = true;
      listDFile(this.queryParams).then(response => {
        this.DFileList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        fileArchiveName: null,
        archiveType: null,
        enterpriseId: null,
        departmentId: null,
        userId: null,
        fileArchiveContent: null,
        fileArchiveAddress: null,
        docName: null,
        docNumber: null,
        saveTime: null,
        borrowState: null,
        secrecyLevel: null,
        boxNumber: null,
        location: null,
        createTime: null,
        shenpiState: null,
        enterpriseAdminId: null,
        enterpriseAdminName: null,
        enterpriseAdminOpinion: null,
        enterpriseAdminNote: null,
        governmentAdminId: null,
        governmentAdminName: null,
        governmentAdminOpinion: null,
        governmentAdminNote: null,
        createBy: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "档案预归档";
    },
    /** 修改按钮操作 */
    handleUpdateEnterprise(row) {
      // 未初审且待初审
      if(row.shenpiState == 0 && row.enterpriseAdminOpinion == 0){
        this.reset();
        const id = row.id || this.ids;
        this.open = true;
        getDFile(id).then(response => {
          this.form = response.data;
          this.form.enterpriseAdminId  = this.user.userId;
          this.form.enterpriseAdminName  = this.user.userName;
          this.title = "企业审批档案预归档";
        });
      }else {
        this.$modal.msgError("非待审批状态！");
      }
    
    },
    handleUpdateGovernment(row) {
       // 未复审且初审通过
       if(row.enterpriseAdminOpinion == 1 && row.governmentAdminOpinion == 0){
        this.reset();
        const id = row.id || this.ids
        this.open = true;
        getDFile(id).then(response => {
          this.form = response.data;
          this.form.governmentAdminId  = this.user.userId;
          this.form.governmentAdminName  = this.user.userName;
          this.title = "政府审批档案预归档";
        });
       }
  
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            this.form.shenpiState = this.form.governmentAdminOpinion;
            updateDFile(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDFile(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除档案预归档编号为"' + ids + '"的数据项？').then(function() {
        return delDFile(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/DFile/export', {
        ...this.queryParams
      }, `DFile_${new Date().getTime()}.xlsx`)
    },
     // 文件路径处理
    getFilePath(filePaths) {
      var arrayPath = filePaths.split(",");
      for (let i in arrayPath) {
        arrayPath[i] = this.baseUrl + arrayPath[i];
      }
      return arrayPath;
    },

    // 获取文件名称
    getFileName(name) {
      if (name.lastIndexOf("/") > -1) {
        return name.slice(name.lastIndexOf("/") + 1).toLowerCase();
      } else {
        return "";
      }
    },
  }
};
</script>
